﻿using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtyManagementSystem.BLL;
using RealtyManagementSystem.BLL.Fees;
using RealtyManagementSystem.DAL;

namespace RealtyManagementSystem.Web.RealtyManagers
{
    public partial class PayPropertiesFee : System.Web.UI.Page
    {
        private PropertyFeeBill _feeBill = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAll();
                InitData(sender, e);
            }
        }

        private void BindAll()
        {
            BindBuildings();
            BindUnits();
            BindNumbers();
        }

        private void InitData(object sender, EventArgs e)
        {
            using (realtydbEntities context = new realtydbEntities())
            {
                var bId = Convert.ToInt16(ddlBuilding.SelectedValue);
                var uNo = Convert.ToByte(ddlUnit.SelectedValue);
                var dNo = Convert.ToInt16(ddlNumber.SelectedValue);

                var householder = (from holder in context.Households
                                   where holder.Building.BuildingId == bId
                                        && holder.UnitNo == uNo
                                        && holder.DepartmentNo == dNo
                                   select holder).SingleOrDefault();
                if (householder == null)
                {
                    lblFeeAmount.Text = "用户不存在";
                    lblFeeDetail.Text = "用户不存在";
                    BindAll();
                    return;
                }
                //TODO 年从用户创建时开始
                _feeBill = new PropertyFeeBill((from i in Enumerable.Range(1988, DateTime.Now.Year - 1987)
                                                select i).ToList(), householder);

                this.txtPaidUp.Text = _feeBill.TotalPaidUp() + "元";
                this.lblFeeAmount.Text = _feeBill.Cost() + "元";
                this.lblFeeDetail.Text = _feeBill.GetDetails().ToString();
            }
        }

        private void BindBuildings()
        {
            var buildings = new BuildingsProvider();
            ddlBuilding.DataSource = buildings.GetBuildings();
            ddlBuilding.DataValueField = "BuildingId";
            ddlBuilding.DataTextField = "BuildingName";
            ddlBuilding.DataBind();
        }

        private void BindUnits()
        {
            var units = (from u in Enumerable.Range(1, 5)
                         select new ListItem(u.ToString(), u.ToString())).ToList();
            this.ddlUnit.DataSource = units;
            this.ddlUnit.DataBind();
        }

        private void BindNumbers()
        {
            byte uNo = ddlUnit.SelectedValue == "" ? (byte)1 : Convert.ToByte(ddlUnit.SelectedValue);
            short bNo = ddlBuilding.SelectedValue == "" ? (short)2 : Convert.ToInt16(ddlBuilding.SelectedValue);

            using (realtydbEntities context = new realtydbEntities())
            {
                var numbers = (from u in context.Households
                               where u.UnitNo == uNo
                                    && u.BuildingId == bNo
                               select new ListItem()
                               {
                                   Text = SqlFunctions.StringConvert((double)u.DepartmentNo),
                                   Value = SqlFunctions.StringConvert((double)u.DepartmentNo)
                               }).ToList();
                this.ddlNumber.DataSource = numbers;
                this.ddlNumber.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (realtydbEntities context = new realtydbEntities())
            {
                var bId = Convert.ToInt16(ddlBuilding.SelectedValue);
                var uNo = Convert.ToByte(ddlUnit.SelectedValue);
                var dNo = Convert.ToInt16(ddlNumber.SelectedValue);

                var householder = (from holder in context.Households
                                   where holder.Building.BuildingId == bId
                                        && holder.UnitNo == uNo
                                        && holder.DepartmentNo == dNo
                                   select holder).SingleOrDefault();
                if (householder != null)
                {
                    var paid = Convert.ToSingle(this.txtPaidUp.Text);
                    var standard = (from s in context.FeeStandards
                                    where s.Year == DateTime.Now.Year && s.Category == (short)1
                                    select s).SingleOrDefault();
                    var fee = new PropertyFee()
                    {
                        FeeStandard = standard,
                        Household = householder,
                        ReceivedAmount = paid
                    };
                    context.AddToPropertyFees(fee);
                    context.SaveChanges();
                }
            }
        }

        protected void ddlBuilding_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindNumbers();
        }

        protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindNumbers();
        }
    }//class
}